package com.example.onlinemonitoring.Iec104.common;

import java.util.ArrayList;
import java.util.List;

import com.example.onlinemonitoring.Iec104.enums.QualifiersEnum;
import com.example.onlinemonitoring.Iec104.enums.TypeIdentifierEnum;
import com.example.onlinemonitoring.Iec104.enums.UControlEnum;
import com.example.onlinemonitoring.Iec104.message.MessageDetail;
import com.example.onlinemonitoring.Iec104.message.MessageInfo;
import com.example.onlinemonitoring.Iec104.util.ByteUtil;
import com.example.onlinemonitoring.Iec104.util.Iec104Util;

/**
 * 104 规约的基本指令封装
 * 
 * @ClassName: BasicInstruction104
 * @Description: 返回指定的指令
 * @author YDL
 */

public class BasicInstruction104 {
	// 68040B 00 00 00
	/**
	 * 初始确认指令
	 */
	public static final byte[] STARTDT_YES = new byte[] { 0x68, 0x04, 0x0B, 0x00, 0x00, 0x00 };

	/**
	 * 链路启动指令
	 */
	public static final byte[] STARTDT = new byte[] { 0x68, 0x04, 0x07, 0x00, 0x00, 0x00 };

	/**
	 * 测试确认
	 */
	public static final byte[] TESTFR_YES = new byte[] { 0x68, 0x04, (byte) 0x83, 0x00, 0x00, 0x00 };

	/**
	 * 测试命令指令
	 */
	public static final byte[] TESTFR = new byte[] { 0x68, 0x04, (byte) 0x43, 0x00, 0x00, 0x00 };

	/**
	 * 停止确认
	 */
	public static final byte[] STOPDT_YES = new byte[] { 0x68, 0x04, 0x23, 0x00, 0x00, 0x00 };

	/**
	 * 
	 * @Title: getGeneralCallRuleDetail104
	 * @Description: 总召唤指令
	 * @param @return
	 * @param @throws IOException
	 * @return MessageDetail
	 * @throws
	 */
	public static MessageDetail getGeneralCallRuleDetail104() {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.generalCall;
		int sq = 0;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 0;
		// 发送序号
		short send = 0;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 6;
		boolean isTest = false;
		boolean isPn = true;
		// 终端地址 实际发生的时候会被替换
		short terminalAddress = 1;
		// 消息地址 总召唤地址为0
		int messageAddress = 0;
		QualifiersEnum qualifiers = QualifiersEnum.generalCallGroupingQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		// MessageInfo message = new MessageInfo();
		// message.setQualifiersType(qualifiers);
		// message.setMessageInfos(new byte[] {});
		// messages.add(message);
		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPn,
				transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);
		return ruleDetail104;
	}

	/**
	 * 
	 * @Title: getYesGeneralCallRuleDetail104
	 * @Description: 总召唤确认指令
	 * @return
	 * @return MessageDetail
	 * @throws
	 */
	public static MessageDetail getYesGeneralCallRuleDetail104() {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.generalCall;
		// SQ=0 length =1
		int sq = 0;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 0;
		// 发送序号
		short send = 0;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 7;
		// true：1 ; false ： 0
		boolean isTest = false;
		// true:0 false;1
		boolean isPN = true;

		short terminalAddress = 1;
		// 消息地址 总召唤地址为0
		int messageAddress = 0;

		QualifiersEnum qualifiers = QualifiersEnum.generalCallGroupingQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		MessageInfo message = new MessageInfo();
		message.setQualifiersType(qualifiers);
		message.setMessageInfos(new byte[] { 1 });

		messages.add(message);
		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPN,
				transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);
		return ruleDetail104;
	}

	/**
	 *
	 * @Title: getYesGeneralCallRuleDetail104
	 * @Description: 总召唤确认指令 2 激活确认 测试
	 * @return
	 * @return MessageDetail
	 * @throws
	 */
	public static MessageDetail getYesGeneralCall2RuleDetail104() {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.generalCall;
		// SQ=0 length =1
		int sq = 1;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 0;
		// 发送序号
		short send = 0;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 0x07;
		// true：1 ; false ： 0
		boolean isTest = false;
		// true:0 false;1
		boolean isPN = true;

		short terminalAddress = 1;
		// 消息地址 总召唤地址为0
		int messageAddress = 0;
		// 老板限定词
		// QualifiersEnum qualifiers = QualifiersEnum.generalCallGroupingQualifiers;
		// List<MessageInfo> messages = new ArrayList<>();
		QualifiersEnum qualifiers = QualifiersEnum.generalCallQualifiers;
		List<MessageInfo> messages = new ArrayList<>();

		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPN,
				transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);
		return ruleDetail104;
	}

	/**
	 * 
	 * @Title: getEndGeneralCallRuleDetail104
	 * @Description: 总召唤结束指令
	 * @return
	 * @return MessageDetail
	 * @throws
	 */
	public static MessageDetail getEndGeneralCallRuleDetail104() {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.generalCall;
		// SQ=0 length =1
		int sq = 0;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 1;
		// 发送序号
		short send = 4;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 0x0A;
		// true：1 ; false ： 0
		boolean isTest = false;
		// true:0 false;1
		boolean isPN = true;

		short terminalAddress = 1;
		// 消息地址 总召唤地址为0
		int messageAddress = 0;
		// 老板限定词
		// QualifiersEnum qualifiers = QualifiersEnum.generalCallGroupingQualifiers;
		// List<MessageInfo> messages = new ArrayList<>();
		QualifiersEnum qualifiers = QualifiersEnum.qualityQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		// MessageInfo message = new MessageInfo();
		// message.setMessageAddress(2);
		// message.setMessageInfos(new byte[] {0x12, 0x34});
		// messages.add(message);
		//
		// MessageInfo message2 = new MessageInfo();
		// message.setMessageAddress(3);
		// message2.setMessageInfos(new byte[] {0x56, 0x78});
		// messages.add(message2);
		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPN,
				transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);
		return ruleDetail104;
	}

	// 响应总召唤 遥测数据
	public static MessageDetail getYaoCeDetail104() {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.shortFloatingPointTelemetry;
		// SQ=0 length =1
		int sq = 1;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 1;
		// 发送序号
		short send = 4;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 0x14;
		// true：1 ; false ： 0
		boolean isTest = false;
		// true:0 false;1
		boolean isPN = true;
		short terminalAddress = 1;
		int messageAddress = 100;
		// 老板限定词
		QualifiersEnum qualifiers = QualifiersEnum.qualityQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		MessageInfo message = new MessageInfo();
		message.setMessageAddress(2);
		ByteUtil.float2byte(36.5f);
		message.setMessageInfos(ByteUtil.float2byte(35.5f));
		messages.add(message);

		MessageInfo message2 = new MessageInfo();
		message2.setMessageAddress(3);
		message2.setMessageInfos(ByteUtil.float2byte(36.5f));
		messages.add(message2);

		MessageInfo message3 = new MessageInfo();
		message3.setMessageAddress(4);
		message3.setMessageInfos(ByteUtil.float2byte(37.5f));
		messages.add(message3);

		MessageInfo message4 = new MessageInfo();
		message4.setMessageAddress(5);
		message4.setMessageInfos(ByteUtil.float2byte(36.3f));
		messages.add(message4);

		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPN,
				transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);
		return ruleDetail104;
	}

	// 响应总召唤 测试 遥信数据
	public static MessageDetail getRSPYAOXinDetail104() {
		TypeIdentifierEnum typeIdentifierEnum = TypeIdentifierEnum.onePointTeleindication;
		// SQ=0 length =1
		int sq = 1;
		boolean isContinuous = sq == 0 ? false : true;
		// 接收序号
		short accept = 1;
		// 发送序号
		short send = 4;
		byte[] control = Iec104Util.getIcontrol(accept, send);
		// 传输原因
		short transferReason = 0x14;
		// true：1 ; false ： 0
		boolean isTest = false;
		// true:0 false;1
		boolean isPN = true;
		short terminalAddress = 1;
		int messageAddress = 1;
		// 老板限定词
		QualifiersEnum qualifiers = QualifiersEnum.qualityQualifiers;
		List<MessageInfo> messages = new ArrayList<>();
		MessageInfo message = new MessageInfo();
		message.setMessageAddress(2);
		message.setMessageInfos(new byte[] { 0 });
		messages.add(message);

		MessageInfo message2 = new MessageInfo();
		message2.setMessageAddress(3);
		message2.setMessageInfos(new byte[] { 0 });
		messages.add(message2);

		MessageInfo message3 = new MessageInfo();
		message3.setMessageAddress(4);
		message3.setMessageInfos(new byte[] { 1 });
		messages.add(message3);

		MessageInfo message4 = new MessageInfo();
		message4.setMessageAddress(5);
		message4.setMessageInfos(new byte[] { 1 });
		messages.add(message4);

		MessageDetail ruleDetail104 = new MessageDetail(control, typeIdentifierEnum, isContinuous, isTest, isPN,
				transferReason,
				terminalAddress, messageAddress, messages, null, qualifiers);
		return ruleDetail104;
	}

	public static MessageDetail getInitRuleDetail104() {
		byte[] control = ByteUtil.intToByteArray(UControlEnum.STARTDT.getValue());
		MessageDetail ruleDetail104 = new MessageDetail(control);
		return ruleDetail104;
	}
}
